#include "PuzzleGameUtils.h"  
#include <algorithm>
#include "PuzzleGuess.h" 
#include "cocos2d.h"

NS_GAME_BEGIN

UniqueRandom::UniqueRandom():
randomSeeds()
,nextRandomIndex(0)
{

}

UniqueRandom::~UniqueRandom()
{}

void UniqueRandom::setSeed(int begin,int end)
{ 
	this->randomSeeds.clear();
	int randomSeedSize = end-begin+1;
	this->randomSeeds.resize(randomSeedSize); 
	for(int x=begin;x<=end;x++)
	{
		this->randomSeeds.push_back(std::pair<int,int>(x, rand()) );
	}
	
	std::sort(randomSeeds.begin(), randomSeeds.end(), [](const std::pair<int,int> a, const std::pair<int,int> b){
		return a.second > b.second;
    });
	this->nextRandomIndex = 0;
}

int UniqueRandom::nextRandom()
{
	if(this->nextRandomIndex>=this->randomSeeds.size())
	{
		this->nextRandomIndex = 0;
	} 
	return this->randomSeeds.at(this->nextRandomIndex++).first;
}

int PuzzleGameUtils::randomInt(int minNum,int maxNum )
{ 
	return minNum + rand()%(maxNum-minNum + 1);
}

bool PuzzleGameUtils::hitTest(cocos2d::Node *node,cocos2d::Touch *touch)
{
	auto touchPoint = node->convertTouchToNodeSpace(touch);
	auto contentSize = node->getContentSize();
	auto contentRect = cocos2d::Rect(0,0,contentSize.width,contentSize.height);
	auto isContainsPoint = contentRect.containsPoint(touchPoint);
	return isContainsPoint;
}

float PuzzleGameUtils::getDistance(cocos2d::Point p1,cocos2d::Point p2)
{
	return sqrt((pow((p2.y-p1.y),2) + pow((p2.x-p1.x),2)));
}



NS_GAME_END